<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"></el-step>
      <el-step title="创建课程大纲"></el-step>
      <el-step title="最终发布"></el-step>
    </el-steps>

    <el-button type="text" @click="openChapterDialog">添加章节</el-button>
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title" />
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 章节 -->
    <ul class="chanpterList">
      <li v-for="chapter in chapterVideoList" :key="chapter.id">
        <p>
          {{ chapter.title }}
          {{ chapter.id }}

          <span class="acts">
            <el-button type="text">添加课时</el-button>
            <el-button style="" type="text" @click="editChapter(chapter.id)">编辑</el-button>
            <el-button type="text" @click="deleteChapter(chapter.id)">删除</el-button>
          </span>
        </p>

        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li v-for="video in chapter.videoLists" :key="video.id">
            <p>{{ video.title }}
              <span class="acts">
                <el-button type="text">编辑</el-button>
                <el-button type="text">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import chapterService from '@/api/edu/chapter'
  export default {
    data() {
      return {
        active: 0,
        saveBtnDisabled: false, // 保存按钮是否禁用
        courseId: 0,
        chapterVideoList: [],
        dialogChapterFormVisible: false,
        chapter: {
          title: '',
          sort: 0
        }
      };
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        if (this.$route.params && this.$route.params.id) {
          this.courseId = this.$route.params.id
          this.getChapterVideo(this.courseId);
        } else {
          this.chapterVideoList = {}
        }
      },
      // 添加章节
      addChapter() {
        // 获取课程id
        this.chapter.courseId = this.courseId
        // 添加章节
        chapterService.addChapter(this.chapter).then(response => {
          this.dialogChapterFormVisible = false
          this.$message({
            type: 'success',
            message: '添加章节成功'
          })
          // 要写在addChapter方法里面，执行完才会执行getChapterVideo
          this.getChapterVideo()
        })
      },
      // 修改章节
      editChapter(chapterId) {
        // alert(chapterId)
        chapterService.getChapterById(chapterId).then(response => {
          this.chapter = response.data.eduChapter
          this.dialogChapterFormVisible = true
          console.log(this.chapter)
        })
      },
      // 更新章节
      updateChapter() {
        chapterService.updateChapter(this.chapter).then(response => {
          this.dialogChapterFormVisible = false
          this.$message({
            type: "success",
            message: "修改章节成功！"
          });
          this.getChapterVideo()
        })
      },
      // 保存章节
      saveOrUpdate() {
        if (!this.chapter.id) {
          this.addChapter()
        } else {
          this.updateChapter()
        }
      },
      // 删除章节
      deleteChapter(chapterId) {

        const h = this.$createElement;
        // this.$confirm('此操作将删除章节, 是否继续?', '警告', {
        this.$confirm(h('h2', {
          style: 'color: teal'
        }, '此操作将删除章节, 是否继续?'), '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => { // 点击确定执行 then 方法
          chapterService.deleteChapter(chapterId).then(response => {
            this.$message({
              type: "success",
              message: "删除章节成功！"
            })
            this.getChapterVideo()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除！'
          })
        })
      },
      // 获取所有章节小节
      getChapterVideo() {
        chapterService.getChapterVideo(this.courseId).then(response => {
          this.chapterVideoList = response.data.chapterVideoList
          console.log(this.chapterVideoList)
        })
      },
      // 每次打开弹框时清空表单
      openChapterDialog() {
        this.dialogChapterFormVisible = true
        this.chapter.title = ''
        this.chapter.sort = 0
      },
      previous() {
        this.$router.push('/course/info/' + this.courseId)
      },
      next() {
        this.$router.push('/course/public/' + this.courseId)
      },
    }
  }
</script>
<style scoped>
  .chanpterList {
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .chanpterList li {
    position: relative;
  }

  .chanpterList p {
    /* float: left; */
    font-size: 20px;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    border: 1px solid #DDD;
  }

  .chanpterList .acts {
    float: right;
    font-size: 14px;
  }

  .videoList {
    padding-left: 50px;
  }

  .videoList p {
    /* float: left; */
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    border: 1px dotted #DDD;
  }
</style>
